package br.ufpe.cin.rdfilter.model;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.hp.hpl.jena.graph.query.regexptrees.BackReference;

public class Manager {

	
	private EntityManagerFactory emf;
	private EntityManager em;

	public Manager(){
		emf = Persistence.createEntityManagerFactory("RDFilter");
		em = emf.createEntityManager();
	}
	
	public void addUser(String user){	
		User u = new User();
		u.setName(user);
		em.getTransaction().begin();
		em.persist(u);
		em.getTransaction().commit();
		
	}
	
	public User getUser(String name){
		Query q = em.createNamedQuery("User.findUser");
		q.setParameter(1,name);
		User u = (User) q.getResultList().get(0);
		return u;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getUsers() {
		Query q = em.createNamedQuery("User.findAll");
		List<User> list = q.getResultList();
	return list;
}
	
	public void addQuery(String label, String description, String statement) {
		br.ufpe.cin.rdfilter.model.Query q = new br.ufpe.cin.rdfilter.model.Query();
		q.setLabel(label);
		q.setDescription(description);
		q.setStatement(statement);
		em.getTransaction().begin();
		em.persist(q);
		em.getTransaction().commit();
	}

	public br.ufpe.cin.rdfilter.model.Query getQuery(String label){
		Query q = em.createNamedQuery("Query.findQuery");
		q.setParameter(1,label);
		br.ufpe.cin.rdfilter.model.Query query = (br.ufpe.cin.rdfilter.model.Query) q.getResultList().get(0);
		return query;
	}
	
	@SuppressWarnings("unchecked")
	public List<br.ufpe.cin.rdfilter.model.Query> getQueries() {
		Query q = em.createNamedQuery("Query.findAll");
		List<br.ufpe.cin.rdfilter.model.Query> list = q.getResultList();
	return list;
}
	
	@SuppressWarnings("unchecked")
	public List<br.ufpe.cin.rdfilter.model.Query> getSomeQueries() {
		Query q = em.createNamedQuery("Query.findSome");
		List<br.ufpe.cin.rdfilter.model.Query> list = q.getResultList();
	return list;
}
	
	public void addSource(String label, String url) {
		Source s = new Source();
		s.setLabel(label);
		s.setUrl(url);
		em.getTransaction().begin();
		em.persist(s);
		em.getTransaction().commit();
	}

	public Source getSource(String label){
		Query q = em.createNamedQuery("Source.findSource");
		q.setParameter(1,label);
		Source s = (Source) q.getResultList().get(0);
		return s;
	}
	
	@SuppressWarnings("unchecked")
	public List<Source> getSources() {
		Query q = em.createNamedQuery("Source.findAll");
		List<Source> list = q.getResultList();
	return list;
}

	public void addFeedback(br.ufpe.cin.rdfilter.model.Query q, String term, String type, User u) {
		Feedback f = new Feedback();
		
		f.setQueryBean(q);
		
		f.setTerm(term);
		f.setType(type);
		f.setUserBean(u);
		em.getTransaction().begin();

		em.persist(f);
		em.getTransaction().commit();
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Feedback> getFeedback() {
		Query q = em.createNamedQuery("Feedback.findAll");
		List<Feedback> list = q.getResultList();
	return list;
}
	
	@SuppressWarnings("unchecked")
	public List<Feedback> getFeedback(br.ufpe.cin.rdfilter.model.Query query) {
		Query q = em.createNamedQuery("Feedback.findByQuery");
		q.setParameter(1, query);
		List<Feedback> list = q.getResultList();
	return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Feedback> getAnnotation(User u) {
		Query q = em.createNamedQuery("Feedback.findAnnotation");
		List<Feedback> list = q.getResultList();
	return list;
}
	
	@SuppressWarnings("unchecked")
	public List<Feedback> getExp(User u) {
		Query q = em.createNamedQuery("Feedback.findExp");
		q.setParameter(1, u);
		List<Feedback> list = q.getResultList();
	return list;
}
	
	@SuppressWarnings("unchecked")
	public List<Feedback> getNonExp(User u) {
		Query q = em.createNamedQuery("Feedback.findNonExp");
		q.setParameter(1, u);
		List<Feedback> list = q.getResultList();
	return list;
}
	
	public void addSimilarity(Double measure, User user1, User user2){
		Similarity s = new Similarity();
		s.setMeasure(measure);
		s.setUser1Bean(user1);
		s.setUser2Bean(user2);
		em.getTransaction().begin();
		em.persist(s);
		em.getTransaction().commit();
	}
	
	public void addTerm(String term){
		Term t = new Term();
		t.setLabel(term);
		em.getTransaction().begin();
		em.persist(t);
		em.getTransaction().commit();
	}
	
	public void addTerm(Term t){
		em.getTransaction().begin();
		em.persist(t);
		em.getTransaction().commit();
	}
	
	public Term getTerm(String label){
		Query q = em.createNamedQuery("Term.findTerm");
		q.setParameter(1,label);
		Term u = (Term) q.getResultList().get(0);
		return u;
	}
	
	public void stop() {
		em.close();
		emf.close();
	}
}
